package cn.shenlong.portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.shenlong.portal.domain.User;
import cn.shenlong.portal.service.UserService;

@Controller
public class IndexAController {

	@Autowired
	private UserService userService;

	@RequestMapping("/login")
	public String login() {
		return "/server/index/login";
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "/server/index/login";
	}

	@ResponseBody
	@RequestMapping("/login/ajaxPassword")
	public Map<String, Object> ajaxPassword(String registName, String password,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (StringUtil.isBlank(registName) || StringUtil.isBlank(password)) {
			resultMap.put("msg", "false");
			return resultMap;
		}
		List<User> result = userService.findUserByRegistName(registName);
		if (result != null && result.size() > 0) {
			if (result.get(0).getPassword().trim().equals(password)) {
				resultMap.put("msg", "success");

				request.getSession().setAttribute("admin_user", result.get(0));
				request.getSession().setAttribute("admin_user_name",
						result.get(0).getUserName());

			} else {
				resultMap.put("msg", "false");
			}

		} else {
			resultMap.put("msg", "false");
		}

		return resultMap;
	}

	@RequestMapping("/admin/index")
	public String index() {
		return "/server/index/index";
	}

}
